package com.nmcc.app_adherence;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class HousesListActivity extends Activity {
		private static ListView lstvwHouseList;
		private Spinner spnHousesx;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_houses_list);
		
		
		Button b1 = (Button) findViewById(R.id.btnnew_house);
        b1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
        	   newhousego(arg0);
        	   
        	   //addItemsOnListViewHouseToEdit();
           }
        });
        
        
        
        //
    	Button b15 = (Button) findViewById(R.id.btnEditSelectedHouse);
        b15.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
        	   
        	   EditSelectedHouse(arg0);
      
           }
        });       

        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.houses_list, menu);
		return true;
	}
	
	@Override
	protected void onStart(){
		super.onStart();
		//addItemsOnListViewHouseToEdit();
		ListTheHouses();
	}
	
	//int givenId, String comment, int housenumber, String placename
	public void addItemsOnListViewHouseToEdit() {
    	MySQLiteHelper db = new MySQLiteHelper(this);
    	
    	spnHousesx = (Spinner) findViewById(R.id.spnHouses);
    	//lstvwHouseList=(ListView) findViewById(R.id.houses_listview);
      List list = new ArrayList();
      //add all the house get name personid
      //for loop
     
      String hashouses="no";
      for (int i = 1000; i >= 0; i--) {//change the maximum. start with the latest up to 500 houses
      	
    	  String gethousedetails=db.GetHouseNameAndIDForListing(i).toString();
 	     // list.add(gethousedetails);
    	  if (gethousedetails.equals("0"))
             {
        	     //do nothing
        	 }
	     else
            {
	    	 hashouses="yes";
	    	 list.add(gethousedetails);
        	 }
 	      
      }

	          if (hashouses.equals("yes"))
	          {
	        	ArrayAdapter dataAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice, list);
	    	     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	     // spnHouseEditspn.setAdapter(dataAdapter);
	        	spnHousesx.setAdapter(dataAdapter);

	     	  }
      
      
    }	    

	
	public void ListTheHouses()
    {
	   	MySQLiteHelper mh = new MySQLiteHelper(this);
	   	MySQLiteHelper db1 = new MySQLiteHelper(this);
	   	
		SQLiteDatabase db = mh.getReadableDatabase();
		
		//String hs_id = db1.GetSetting(3);

		spnHousesx = (Spinner) findViewById(R.id.spnHouses);
		
      List list = new ArrayList();
     
      String hashouses="no";
      
  	String comment;
	String placename;
	int housenumber;
	int id;
	String DateTaken;
		
    //}
 	
 		//now add the data, same order as above, check corresponding fields
    	String valueToReturn="0";
	      String table = "GPSData";
	         String[] columns = {"id","comment","HouseNumber","Placename", "HouseID","DateTaken"};
	         //String where = "id=" + givenId;//this is default so its acts as variable to be replaced
	         //String[] args = {"tagname", "tagname", "tagname", "tagname", "tagname"};
	         Cursor rs = db.query(table, columns, null, null, null, null, "DateTaken DESC");

      if(rs.moveToFirst()){
           do{
	        	comment= rs.getString(rs.getColumnIndex("comment"));
	        	placename= rs.getString(rs.getColumnIndex("Placename"));
	        	housenumber= rs.getInt(rs.getColumnIndex("HouseNumber"));
	        	String HouseID= rs.getString(rs.getColumnIndex("HouseID"));
	        	id= rs.getInt(rs.getColumnIndex("id"));
	        	DateTaken= rs.getString(rs.getColumnIndex("DateTaken"));
	     
	        	if (id>0)
               {
	        		valueToReturn= housenumber + ", " + comment + ", || " + placename + ", " + DateTaken + "                                                                                        " + HouseID;
	        		 hashouses="yes";
      	 	    	 list.add(valueToReturn);
      	       }

             }while(rs.moveToNext());
         }
 	
				      if (hashouses.equals("yes"))
				      {
				    	ArrayAdapter dataAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice, list);
					     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					     // spnHouseEditspn.setAdapter(dataAdapter);
				    	spnHousesx.setAdapter(dataAdapter);
				
				 	  }
 	 
       rs.close();
       db.close();

 	   //SendFileViaBluetoothFromSDCardFolder(view, "GPSData.csv");
 	}



	private String getLast50CharactersOfChoicesAsValue(String gnstr) {
		 //get _01 in say 'Head    _01'	 	   last3 = str.substring(str.length() - 3);
	       //String relat=String.valueOf(spnRelations1.getSelectedItem());
	       //check if its first option 'Select Gender.....  _00', then return null
	      
		 String lastcharacterUseForCompare = "0";
		 String Extractedlastcharacter = gnstr.substring(gnstr.length() - 1);
		 
		 if (lastcharacterUseForCompare==Extractedlastcharacter){
	    	   return null;
	      } else {
	    		   return gnstr.substring(gnstr.length() - 50);
	    		  
	       }
	    	   
	 }      
	

	private void EditSelectedHouse(View view) {

		 	   MySQLiteHelper db = new MySQLiteHelper(this);
		
		        Spinner spnHousesCC;
		     spnHousesCC= (Spinner) findViewById(R.id.spnHouses);
		
		   	    String selectedHouseID = getLast50CharactersOfChoicesAsValue(String.valueOf(spnHousesCC.getSelectedItem()));
			   
		   	 String st=selectedHouseID.trim();
		   	 
			//Toast.makeText(getApplicationContext(), set, Toast.LENGTH_LONG).show();

			//String trimedHouseid = selectedHouseID.trim();
			
		   	 

		   	db.addSetting(Integer.parseInt("0"), Integer.parseInt("0"), st,"","");   
		   	   
		    Intent i = new Intent(HousesListActivity.this, HouseActivity.class);
		   startActivity(i);
	    	   
	 }      
	
	
	private void newhousego(View view){
		   
 	   MySQLiteHelper db = new MySQLiteHelper(this);
		
 	   
 	   db.addSetting(0, 0, "","","");
 	   
 	   
    Intent i = new Intent(HousesListActivity.this, HouseActivity.class);
    startActivity(i);

	}
	
}
